package com.mysoft.gfyx.marketdevelop.controller;

import com.google.common.base.Joiner;
import com.mysoft.framework.modeling.dto.Filter;
import com.mysoft.framework.modeling.dto.ListDataResult;
import com.mysoft.framework.modeling.dto.LoadDataParams;
import com.mysoft.framework.modeling.dto.OptionItem;
import com.mysoft.framework.rpc.annotation.PubAction;
import com.mysoft.framework.rpc.annotation.PubService;
import com.mysoft.framework.rpc.contants.RequestPrefix;
import com.mysoft.gfyx.common.dto.GetInfoByIdRequestDto;
import com.mysoft.gfyx.common.dto.ListResult;
import com.mysoft.gfyx.common.util.MyStringUtil;
import com.mysoft.gfyx.marketdevelop.controller.dto.*;
import com.mysoft.gfyx.marketdevelop.mip.GetDeveloperDataService;
import com.mysoft.gfyx.marketdevelop.mip.dto.BaseTjListItemDto;
import com.mysoft.gfyx.marketdevelop.mip.dto.CheckMainDataResponseDto;
import com.mysoft.gfyx.marketdevelop.mip.dto.GetInvestmentRequestDto;
import com.mysoft.gfyx.marketdevelop.mip.dto.TjCustom.GetDevelopersAllTagsRequestDto;
import com.mysoft.gfyx.marketdevelop.mip.dto.TjCustom.SqlApiResponseDto;
import com.mysoft.gfyx.marketdevelop.service.*;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.util.Strings;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import com.mysoft.framework.mvc.api.Controller;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import java.util.*;

@PubService(value = "/developerPub", prefix = RequestPrefix.PUB)
public class DeveloperPubController extends Controller {
    @Autowired
    private DeveloperService developerService;

    @Autowired
    private DeveloperContactService developerContactService;

    @Autowired
    private ProcurementRequirementService procurementRequirementService;

    @Autowired
    private GetDeveloperDataService getDeveloperDataService;

    @Resource
    private SearchService searchService;

    @Resource
    private DeveloperFindSupplierService developerFindSupplierService;

    @Resource
    private DeveloperTakeLandService developerTakeLandService;

    @Resource
    private DeveloperCommencePermitService developerCommencePermitService;

    @Resource
    private OpenedTradingService openedTradingService;

    @Resource
    private DeveloperCooperatorService developerCooperatorService;

    @Resource
    private EnterpriseRiskService enterpriseRiskService;

    @Autowired
    private CompanyService companyService;

    @PubAction(value = "/getMainData")
    public Map<String, Object> getMainData(@RequestBody CreditCodeBaseRequestDto dto) {
        return developerService.getDeveloperMainData(dto.getCreditCode());
    }

    @PubAction(value = "/getMainDataMulti")
    public List<Map<String, Object>> getMainDataMulti(@RequestBody List<String> creditCodeList) {
        ArrayList<BaseTjListItemDto> res = developerService.getDeveloperMainDataMulti(creditCodeList);
        List<Map<String, Object>> listMap = new ArrayList<>();
        res.forEach(r -> {
            Map<String, Object> row = new HashMap<>();
            Map<String, Object> data = new HashMap<>();
            data.put("logo", r.getItem().get("developer_logo").toString());
            data.put("creditCode", r.getItem().get("organizing_code").toString());
            ArrayList tagName = new ArrayList<>();
            r.getTag_infos().forEach(t -> {
                tagName.add(t.get("tag_value").toString());
            });
            data.put("tag", Joiner.on(",").join(tagName));
            data.put("developer_score", r.getItem().get("developer_score"));
            row.put(r.getItem().get("organizing_code").toString(), data);
            listMap.add(row);
        });
        return listMap;
    }

    @PubAction(value = "/getLabelMulti")
    public List<Map<String, Object>> getLabelMulti(@RequestBody List<String> creditCodeList) {
        List<Map<String, Object>> res = new LinkedList<>();
        for (int i = 0; i < creditCodeList.size(); i = i + 100) {
            int max = creditCodeList.size() > (i + 100) ? (i + 100) : creditCodeList.size();
            List<String> sliceCodes = creditCodeList.subList(i, max);
            GetDevelopersAllTagsRequestDto getDevelopersAllTagsRequestDto = new GetDevelopersAllTagsRequestDto();
            getDevelopersAllTagsRequestDto.setOrganization_codes(Strings.join(sliceCodes, ','));

            SqlApiResponseDto sqlApiResponseDto = getDeveloperDataService.getDevelopersAllTags(getDevelopersAllTagsRequestDto);

            res.addAll(sqlApiResponseDto.getData().getRows());
        }

        return res;
    }

    @PubAction(value = "/getPurchaseContacts")
    public List<Map<String, Object>> getPurchaseContacts(@RequestBody GetPurchaseContactsRequestDto dto) {
        return developerContactService.getPurchaseContacts(dto.getCreditCode(), dto.getId());
    }

    @PubAction(value = "/getPurchaseProjects")
    public List<Map<String, Object>> getPurchaseProjects(@RequestBody GetPurchaseContactsRequestDto dto) {
        return procurementRequirementService.getPurchaseProjects(dto.getId());
    }

    @PubAction(value = "/getContactInfo")
    public Map<String, Object> getContactInfo(@RequestBody GetInfoByIdRequestDto dto) {
        return developerContactService.getContactInfo(dto.getId());
    }

    @PubAction(value = "/searchDeveloperSimulator")
    public ListDataResult searchDeveloperSimulator(@RequestBody LoadDataParams options) {
        return searchService.loadSearchDeveloperData(options);
    }

    @PubAction(value = "/getDeveloperMainData")
    public Object getDeveloperMainData(@RequestBody CreditCodeBaseRequestDto dto) {
        return developerService.getDeveloperMainDataAndSummary(dto.getCreditCode());
    }

    @PubAction(value = "/getFeeds")
    @ApiOperation(value = "/getInvestments", notes = "客户详情-客户动态")
    public ListResult<Map<String, Object>> getFeeds(@RequestBody CreditCodeBaseRequestDto dto) {
        return developerService.getDeveloperFeeds(dto.getCreditCode());
    }

    @PubAction(value = "/getDeveloperMetrics")
    public Object getDeveloperMetrics(@RequestBody CreditCodeBaseRequestDto dto) {
        return developerService.getMetrics(dto.getCreditCode());
    }

    @PubAction(value = "/getOpportunityDetail")
    public Object getOpportunityDetail(@RequestBody GetOpportunityDetailRequestDto dto) {
        return developerService.getOpportunityDetail(dto.getId());
    }

    @PubAction(value = "/getCertificates")
    @ApiOperation(value = "/getInvestments", notes = "客户详情-企业资质")
    public Object searchDeveloper(@RequestBody CreditCodeBaseRequestDto dto) {
        return developerService.getCertificates(dto);
    }

    @PubAction(value = "/getInvestments")
    @ApiOperation(value = "/getInvestments", notes = "客户详情-投资公司")
    public Object getInvestments(@RequestBody CreditCodeBaseRequestDto req) {
        GetInvestmentRequestDto dto = new GetInvestmentRequestDto();
        dto.setCredit_code(req.getCreditCode());
        return developerService.getInvestment(dto);
    }

    @PubAction(value = "/getDeveloperDetailRiskCount")
    @ApiOperation(value = "/getDeveloperDetailRiskCount", notes = "客户详情-企业风险数量汇总")
    public Object getDeveloperDetailRiskCount(@RequestBody CreditCodeBaseRequestDto dto) {
        return developerService.getDeveloperDetailRiskCount(dto.getCreditCode());
    }

    @PubAction(value = "/developerContact")
    @ApiOperation(value = "/developerContact", notes = "客户详情-联系人")
    public ListDataResult developerContact(@RequestBody LoadDataParams options) {
        return developerContactService.loadData(options);
    }

    @PubAction(value = "/procurementRequirement")
    @ApiOperation(value = "/procurementRequirement", notes = "客户详情-采购需求")
    public ListDataResult procurementRequirement(@RequestBody LoadDataParams options) {
        return procurementRequirementService.loadData(options);
    }

    @PubAction(value = "/findSupplier")
    @ApiOperation(value = "/findSupplier", notes = "客户详情-找供应商")
    public ListDataResult findSupplier(@RequestBody LoadDataParams options) {
        return developerFindSupplierService.loadData(options);
    }

    @PubAction(value = "/takeLand")
    @ApiOperation(value = "/takeLand", notes = "客户详情-拿地")
    public ListDataResult takeLand(@RequestBody LoadDataParams options) {
        return developerTakeLandService.loadData(options);
    }

    @PubAction(value = "/commencePermit")
    @ApiOperation(value = "/commencePermit", notes = "客户详情-开工")
    public ListDataResult commencePermit(@RequestBody LoadDataParams options) {
        return developerCommencePermitService.loadData(options);
    }

    @PubAction(value = "/openedTrading")
    @ApiOperation(value = "/openedTrading", notes = "客户详情-开盘")
    public ListDataResult openedTrading(@RequestBody LoadDataParams options) {
        return openedTradingService.loadData(options);
    }

    @PubAction(value = "/cooperator")
    @ApiOperation(value = "/cooperator", notes = "合作单位")
    public ListDataResult cooperator(@RequestBody LoadDataParams options) {
        return developerCooperatorService.loadData(options);
    }

    @PubAction(value = "/riskEnforcement")
    @ApiOperation(value = "/riskEnforcement", notes = "失信被执行")
    public ListDataResult riskEnforcement(@RequestBody LoadDataParams options) {
        return enterpriseRiskService.loadEnforcementData(options);
    }

    @PubAction(value = "/caseRegistration")
    @ApiOperation(value = "/caseRegistration", notes = "立案信息")
    public ListDataResult caseRegistration(@RequestBody LoadDataParams options) {
        return enterpriseRiskService.loadCaseRegistrationData(options);
    }

    @PubAction(value = "/lawsuit")
    @ApiOperation(value = "/lawsuit", notes = "法律诉讼")
    public ListDataResult lawsuit(@RequestBody LoadDataParams options) {
        return enterpriseRiskService.loadLawsuitData(options);
    }

    @PubAction(value = "/taxRisk")
    @ApiOperation(value = "/taxRisk", notes = "税收违法")
    public ListDataResult taxRisk(@RequestBody LoadDataParams options) {
        return enterpriseRiskService.loadTaxData(options);
    }

    @PubAction(value = "/companyOption")
    @ApiOperation(value = "/companyOption", notes = "公司下拉选项")
    protected List<OptionItem> companyOption(@RequestBody CreditCodeBaseRequestDto dto) {
        Map<String, String> urlParams = new HashMap<>();
        urlParams.put(CompanyService.companyCodeQueryName, dto.getCreditCode());
        return companyService.load(null, urlParams);
    }

    @PubAction(value = "/checkDeveloperMainData")
    @ApiOperation(value = "/checkDeveloperMainData", notes = "校验开发商是否在主档案")
    public List<CheckMainDataResponseDto> checkDeveloperMainData(@RequestBody CheckDeveloperMainDataRequestDto dto) {
        return developerService.getDeveloperMainDataMultiName(dto.getDeveloperName());
    }

    @PubAction(value = "/applyDeveloperMainData")
    @ApiOperation(value = "/applyDeveloperMainData", notes = "申请反哺开发商主档案接口")
    public Object applyDeveloperMainData(@RequestBody ApplyDeveloperMainDataDto reqDto) {
        if (reqDto.getDevelopers() != null && reqDto.getDevelopers().size() > 0) {
            return developerService.applyDeveloperMainData(reqDto);
        } else if (reqDto.getSupplier() != null && reqDto.getSupplier().size() > 0) {
            return developerService.applySupplierMainData(reqDto);
        } else {
            return null;
        }
    }

    @PubAction(value = "/getDeveloperScoreDetail")
    public Object getDeveloperScoreDetail(@RequestBody GetDeveloperScoreDetailRequest dto) {
        return developerService.getDeveloperScoreDetail(dto.getOrganizing_code());
    }
}
